package graduation.project.mutant.strategy;

import graduation.project.body.Expression;
import graduation.project.body.Mutants;
import graduation.project.body.OriginExpression;
import graduation.project.exception.NonExpressionException;
import graduation.project.testcase.*;
import graduation.project.testcase.strategy.*;
import graduation.project.body.*;
import java.util.ArrayList;

/**
 * CORF means replacing "or" operator by "and" operator.
 * @author robin
 *
 */
public class CORFMutantsGen implements MutantsGenerator {

	public Mutants generateMutants(String expression, int mutantsNum) {
		
		try{
			OriginExpression originEx = new OriginExpression(expression);
			return generateMutants(originEx, mutantsNum);
		}catch(NonExpressionException e){
			System.out.println("CORFMutantsGen: the expression is invalid.");
		}
		return null;
	}

	public Mutants generateMutants(OriginExpression originEx, int num) {
		
		int count = 0;
		String idnf = originEx.getIDNF();
		Mutants result = new Mutants(originEx.getExpression(), "CORF");
		System.out.println(originEx.getExpression());
		ArrayList<Expression> mutantSet = new ArrayList<Expression>();
		
		StringBuffer exp = new StringBuffer();
		exp.append(idnf);
		int orCount = idnf.split("\\+").length - 1;
		int orPos = -1;
		for(int i = 0; i < orCount; i++){
			orPos = exp.indexOf("+", orPos + 1);
			try {
				mutantSet.add(new Expression(exp.deleteCharAt(orPos).toString()));
			} catch (NonExpressionException e) {
				System.out.println("CORFMutantsGen: the expression is invalid.");
				e.printStackTrace();
			}
			exp.insert(orPos, '+');
			
			if(num != -1 && ++count >= num)
				break;
		}
		
		result.setMutants(mutantSet);
		return result;
	}

	public static void main(String[] args){
//		CORFMutantsGen mutG = new CORFMutantsGen();
		DORFMutantsGen mutG = new DORFMutantsGen();
//		ENFMutantsGen mutG = new ENFMutantsGen();
//		HDMutantsGen mutG = new HDMutantsGen();
//		LIFMutantsGen mutG = new LIFMutantsGen();
//		LNFMutantsGen mutG = new LNFMutantsGen();
//		LOFMutantsGen mutG = new LOFMutantsGen();
//		LRFMutantsGen mutG = new LRFMutantsGen();
//		TNFMutantsGen mutG = new TNFMutantsGen();
//		TOFMutantsGen mutG = new TOFMutantsGen();
		Mutants ms = mutG.generateMutants("ab + !cd + !ac", 24);
		MUMCUTGenerator mum = new MUMCUTGenerator();
		TestCases tcs = mum.generateTestCases("ab + !cd + !ac", 24);
		Results result = ms.runTestCases(tcs);
		Result[] rs = result.getRecords();
		for(Result r:rs){
			System.out.println("Displaying:"+r.toString());
		}
		ArrayList<Expression> set = ms.getMutants();
		System.out.println("set.length: " + set.size());
		for(int i = 0; i < set.size(); i++)
			System.out.println("" + i + ", " + set.get(i).getExpression());
	}
}
